package com.itcoon.cloud.framework.webmvc.config.properties;

import com.itcoon.cloud.framework.webmvc.response.ApiResponse;
import com.itcoon.cloud.framework.webmvc.trace.enums.LogSegment;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@ConfigurationProperties(prefix = "itcoon.web")
public class GlobalWebConfigProperties {

    private String baseUrl = "http://localhost:8080";

    private Api[] apis = new Api[]{
            new Api()
    };

    private GlobalResponse response = new GlobalResponse();

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Api[] getApis() {
        return apis;
    }

    public void setApis(Api[] apis) {
        this.apis = apis;
    }


    public GlobalResponse getResponse() {
        return response;
    }

    public void setResponse(GlobalResponse response) {
        this.response = response;
    }

    public static class Api {

        private String prefix = "admin-api";

        private String version = "v1";

        private String serverId = "system";

        private String[] controllers = new String[]{"**.controller.**"};

        private HttpTrace httpTrace = new HttpTrace(generalBasePath());

        private Boolean corsEnable = Boolean.TRUE;



        public Api() {
        }

        public String getPrefix() {
            return prefix;
        }

        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getServerId() {
            return serverId;
        }

        public void setServerId(String serverId) {
            this.serverId = serverId;
        }

        public String[] getControllers() {
            return controllers;
        }

        public void setControllers(String[] controllers) {
            this.controllers = controllers;
        }

        public HttpTrace getHttpTrace() {
            return httpTrace;
        }

        public void setHttpTrace(HttpTrace httpTrace) {
            this.httpTrace = httpTrace;
        }

        public Boolean getCorsEnable() {
            return corsEnable;
        }

        public void setCorsEnable(Boolean corsEnable) {
            this.corsEnable = corsEnable;
        }



        public String generalBasePath(){
            String path = "";
            if(StringUtils.hasText(getPrefix())){
                path = path + "/" + getPrefix();
            }
            if(StringUtils.hasText(getVersion())){
                path = path + "/" + getVersion();
            }
            if(StringUtils.hasText(getServerId())){
                path = path + "/" + getServerId();
            }
            return path;
        }
    }


    public static class GlobalResponse {

        private Class<?> boundClass = ApiResponse.class;

        private HttpStatus bizExceptionHttpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

        public HttpStatus getBizExceptionHttpStatus() {
            return bizExceptionHttpStatus;
        }

        public void setBizExceptionHttpStatus(HttpStatus bizExceptionHttpStatus) {
            this.bizExceptionHttpStatus = bizExceptionHttpStatus;
        }

        public Class<?> getBoundClass() {
            return boundClass;
        }

        public void setBoundClass(Class<?> boundClass) {
            this.boundClass = boundClass;
        }


    }

    public static class HttpTrace {

        /**
         *  启用模式
         */
        private final String basic;
        /**
         * 是否启用traceLog
         */
        private Boolean enable = true;

        /**
         * 简化记录部分
         */
        private List<LogSegment> segments = Collections.emptyList();


        private List<String> excludeUris = Collections.emptyList();

        /**
         * 内容类型黑名单
         */
        private List<MediaType> ignoreContentTypes = Collections.emptyList();


        public HttpTrace(String basic) {
            this.basic = basic;
        }

        public String getBasic() {
            return basic;
        }


        public List<String> getExcludeUris() {
            return excludeUris;
        }

        public void setExcludeUris(List<String> excludeUris) {
            this.excludeUris = excludeUris;
        }

        public List<MediaType> getIgnoreContentTypes() {
            return ignoreContentTypes;
        }

        public void setIgnoreContentTypes(List<MediaType> ignoreContentTypes) {
            this.ignoreContentTypes = ignoreContentTypes;
        }

        public Boolean getEnable() {
            return enable;
        }

        public void setEnable(Boolean enable) {
            this.enable = enable;
        }

        public List<LogSegment> getSegments() {
            return segments;
        }

        public void setSegments(List<LogSegment> segments) {
            this.segments = segments;
        }
    }
}
